package com.springdemo.taskexecutor;

import com.springdemo.Util.HttpClientUtil;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class AsyncTaskService {

    private static ArrayList<String> list=new ArrayList<>();

    @Async
    public void executorAsyncTask(Integer i){
//    synchronized (i) {
        String url="https://www.baidu.com";
         String s=null;
        try {
            s = HttpClientUtil.doGet(url, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            s=null;
        }
        String result = "失败";
      if (null != s && s.indexOf("百度") != -1) {
        result = "成功";
      }
      list.add("访问"+url+i+"百度第" + i + "次:" + "结果为：" + result);
//      System.out.println("访问"+s+i+"百度第" + i + "次:" + "结果为：" + result);
//        }
    }

    @Async
    public void executorAsyncTaskPlus(Integer i){
    System.out.println("执行异步任务1："+(i+1));
    }

    public ArrayList<String> getList() {
        return list;
    }

    public void  out(){

    }

}
